package tw.com.softleader.book.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tw.com.softleader.book.dao.BookDao;
import tw.com.softleader.book.entity.Book;

@Repository
public class BookDaoImpl implements BookDao{

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Book> findAll(){
		Session session = sessionFactory.getCurrentSession();
		List<Book> books = session.createCriteria(Book.class).list();
		return books;
	}
	
	public Book findOne(Long id){
		Session session = sessionFactory.getCurrentSession();
		Book book = (Book) session.get(Book.class, id);
		return book;
	}
	
	public void insert(Book book) {
		Session session = sessionFactory.getCurrentSession();
		session.persist(book);
	}
	
	public void update(Book book){
		Session session = sessionFactory.getCurrentSession();
		session.merge(book);
	}
	
	public void delete(Book book){
		Session session = sessionFactory.getCurrentSession();
		session.delete(book);
	}
}
